﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.DAL;
using Wp.BLL;
using System.Web.UI.HtmlControls;

public partial class Modules_Posts_Controls_PostDetail : System.Web.UI.UserControl
{
    public string PostTextId { get; set; }
    public string GetAvaliableDelete()
    {
        string str = "style='display:none'";
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            str = "style='display:inline'";
        }
        return str;
    }

    public string GetCodeEdit(object id)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            return ("<a href='/admin/Modules/Posts/PostEdit.aspx?Id=" + id + "'  target='_blank'>Edit</a>");
        }
        return "";
    }

    public string[] GetTermRelationshipsInfo(object obj)
    {
        List<TblTermRelationshipsEntity> list = (List<TblTermRelationshipsEntity>)obj;
        string category = "";
        string name = "";
        if ((list != null) && (list.Count > 0))
        {
            category = UrlBuilder.GetCategory(list[0].Slug);
            name = list[0].Name;
        }
        return new string[] { name, category };
    }

    private void LoadComment(int PostID, int ParentID, string Approved)
    {
    }

    private void LoadData()
    {
        TblPostsEntity item = TblPostsManager.CreateInstant().SelectOneByTextId(this.PostTextId, "post", true);
        if (item != null)
        {
            List<TblPostsEntity> list = new List<TblPostsEntity> {
                item
            };
            this.rpt.DataSource = list;
            this.rpt.DataBind();
            this.SEO(item);
            this.LoadTag(item.Id);
            if (item.lsTermRelationships.Count > 0)
            {
                this.LoadViewMore(item.CreatedDate, item.lsTermRelationships[0].Slug);
            }
            TblPostMetaEntity entity2 = TblPostMetaManager.CreateInstant().SelectOne(item.Id, "NumView");
            if (entity2 == null)
            {
                entity2 = new TblPostMetaEntity
                {
                    Id = -1,
                    PostId = item.Id,
                    MetaKey = "NumView",
                    MetaValue = "1"
                };
            }
            else
            {
                entity2.MetaValue = (entity2.MetaValue.ToInt() + 1).ToString();
            }
            TblPostMetaManager.CreateInstant().InsertUpdate(entity2);
            this.LoadComment(item.Id, -1, "approved");
        }
    }

    private void LoadTag(int PostId)
    {
        List<TblTermRelationshipsEntity> list = TblTermRelationshipsManager.CreateInstant().SelectByObjectId(PostId, "posttags");
        this.rptposttags.DataSource = list;
        this.rptposttags.DataBind();
    }

    private void LoadViewMore(DateTime Createdate, string TermTaxonomyTextId)
    {
        int totalRow = 1;
        List<TblPostsEntity> list = TblPostsManager.CreateInstant().SelectTblPostBy("post", "", -1, TermTaxonomyTextId, "category", "", new DateTime?(Createdate), "CreatedDate", "DESC", 10, 1, out totalRow);
        this.rptviewmore.DataSource = list;
        this.rptviewmore.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.PostTextId = CommonHelper.QueryString("PostTextId", "");
        if (!base.IsPostBack)
        {
            this.LoadData();
        }
    }

    protected void rptComment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            TblCommentsEntity dataItem = (TblCommentsEntity)e.Item.DataItem;
            Repeater repeater = (Repeater)e.Item.FindControl("rptchild");
            List<TblCommentsEntity> list = TblCommentsManager.CreateInstant().SelectByPost(new int?(dataItem.PostId), new int?(dataItem.Id), "approved", 0x2710);
            if (list.Count > 0)
            {
                repeater.DataSource = list;
                repeater.DataBind();
            }
        }
    }

    private void SEO(TblPostsEntity item)
    {
        string content = "";
        string option = "";
        string str3 = "";
        if ((item.PostMetas["SEOTitle"] != null) && !item.PostMetas["SEOTitle"].Equals(""))
        {
            content = item.PostMetas["SEOTitle"].ToString() + " - " + TblOptionManager.CreateInstant().GetOption("SEOTitle");
        }
        else
        {
            content = item.Title + '-' + TblOptionManager.CreateInstant().GetOption("SEOTitle");
        }
        if ((item.PostMetas["SEODescription"] != null) && !item.PostMetas["SEODescription"].Equals(""))
        {
            option = item.PostMetas["SEODescription"].ToString();
        }
        else
        {
            option = TblOptionManager.CreateInstant().GetOption("SEODescription");
        }
        if ((item.PostMetas["SEOKeyWord"] != null) && !item.PostMetas["SEOKeyWord"].Equals(""))
        {
            str3 = item.PostMetas["SEOKeyWord"].ToString();
        }
        else
        {
            str3 = TblOptionManager.CreateInstant().GetOption("SEOKeyWord");
        }
        string str4 = TblOptionManager.CreateInstant().GetDomainName() + base.Request.RawUrl;
        string image = UrlBuilder.GetImage(item.ImageUrl, 100);
        this.Page.Title = content;
        this.SetHeaderMetName("description", option);
        this.SetHeaderMetName("keywords", str3);
        this.SetHeaderMetProperty("og:title", content);
        this.SetHeaderMetProperty("og:type", "article");
        this.SetHeaderMetProperty("og:url", str4);
        this.SetHeaderMetProperty("og:image", image);
        this.SetHeaderMetProperty("og:description", option);
        this.SetHeaderMetProperty("og:site_name", "Linh Sơn Phật Gi\x00e1o");
    }

    private void SetHeaderMetName(string Name, string Content)
    {
        HtmlMeta child = new HtmlMeta
        {
            Name = Name,
            Content = Content
        };
        this.Page.Master.FindControl("head").Controls.AddAt(this.Page.Master.FindControl("head").Controls.Count - 1, child);
    }

    private void SetHeaderMetProperty(string Name, string Content)
    {
        HtmlMeta child = new HtmlMeta();
        child.Attributes.Add("property", Name);
        child.Content = Content;
        this.Page.Master.FindControl("head").Controls.AddAt(this.Page.Master.FindControl("head").Controls.Count - 1, child);
    }

    public string StrEdit(object CommentID)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            return (" <a href='/Admin/Modules/Comments/CommentEdit.aspx?Id=" + CommentID + "' target='_blank' >Edit</a>");
        }
        return "";
    }

}